package leetcode.linkList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * leetCode 23
 * k路有序链表合并
 * 思路：分治

 */
public class LinkList {

    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        //利用hasNextXXX()判断是否还有下一输入项
        while (sc.hasNext()) {
            //利用nextXXX()方法输出内容
            String str = sc.next();
            List<Integer> input = Arrays.stream(str.split(",")).map(i -> { return Integer.parseInt(i); }).collect(Collectors.toList());
            System.out.println(Arrays.toString(input.toArray()));
            Integer [] nums = input.toArray(new Integer[input.size()]);
//            new LinkList().lengthOfLIS(nums);
        }
    }
     class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode res = null;
        for(int i = 0;i<lists.length;i++){
            res = mergeTwo(res, lists[i]);
        }
        return res;
    }
    public ListNode mergeTwo(ListNode l1, ListNode l2){
        ListNode head = null;
        ListNode tmp = null;
        while(l1 != null || l2 != null){
            if(l1 != null && l2 != null){
                if(l1.val < l2.val){
                    if(tmp == null){
                        tmp = l1;
                        head = tmp;
                        l1 = l1.next;
                    } else{
                        tmp.next = l1;
                        tmp = tmp.next;
                        l1 = l1.next;
                    }
                } else{
                    if(tmp == null){
                        tmp = l2;
                        head = tmp;
                        l2 = l2.next;
                    } else{
                        tmp.next = l2;
                        tmp = tmp.next;
                        l2 = l2.next;
                    }
                }
            }
            else if(l1!=null&&l2==null){

                if(tmp==null){
                    tmp = l1;
                    head = tmp;
                    return head;
                }else{
                    tmp.next = l1;
                    return head;
                }
            }else{
                if(tmp==null){
                    tmp = l2;
                    head = tmp;
                    return head;
                }else{
                    tmp.next = l2;
                    return head;
                }
            }
            }
        return head;
    }
}
